package com.zy.explore.demo.interfaces.assembler;


import com.zy.explore.demo.domain.entity.SysUser;
import com.zy.explore.demo.interfaces.frm.SysUserFrm;
import com.zy.explore.demo.interfaces.vo.SysUserVO;

import com.zy.explore.frm.SortFieldFrm;
import com.zy.explore.util.DataBeanUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 系统用户表(表sys_user)数据实体转换辅助类
 *
 * @author ZY
 * @date 2021-05-05 23:07:39
 */
public class SysUserAssembler {

    /**
     * 私有化构造器
     */
    private SysUserAssembler() {
    }

    /**
     * 实体类转Frm
     *
     * @param entity 实体类
     * @return 转换后的对象
     */
    public static SysUserFrm covertToFrm(SysUser entity) {
        return DataBeanUtils.copyOf(entity, SysUserFrm.class, true);
    }

    /**
     * 实体类转VO
     *
     * @param entity 实体类
     * @return 转换后的对象
     */
    public static SysUserVO convertToVO(SysUser entity) {
        return DataBeanUtils.copyOf(entity, SysUserVO.class, true);
    }

    /**
     * Frm转实体类
     *
     * @param frm 参数对象
     * @return 转换后的实体类对象
     */
    public static SysUser convertFromFrm(SysUserFrm frm) {
        return DataBeanUtils.copyOf(frm, SysUser.class, true);
    }

    /**
     * VO转实体类
     *
     * @param vo 参数对象
     * @return 转换后的实体类对象
     */
    public static SysUser convertFromVO(SysUserVO vo) {
        return DataBeanUtils.copyOf(vo, SysUser.class, true);
    }

    /**
     * Frm转Specification查询条件
     *
     * @param frm 查询参数对象
     * @return 返回JPA所需的查询条件对象
     */
    public static Specification<SysUser> convertToSpecification(SysUserFrm frm) {
        if (frm.getPageNum() == null) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null) {
            frm.setPageSize(10);
        }
        return (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();

            if (frm.getId() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("id"), frm.getId()));
            }
            if (!StringUtils.isEmpty(frm.getName())) {
                predicateList.add(criteriaBuilder.like(root.get("name"), "%" + frm.getName() + "%"));
            }
            if (!StringUtils.isEmpty(frm.getPhone())) {
                predicateList.add(criteriaBuilder.like(root.get("phone"), "%" + frm.getPhone() + "%"));
            }
            if (!StringUtils.isEmpty(frm.getTelephone())) {
                predicateList.add(criteriaBuilder.like(root.get("telephone"), "%" + frm.getTelephone() + "%"));
            }
            if (!StringUtils.isEmpty(frm.getAddress())) {
                predicateList.add(criteriaBuilder.like(root.get("address"), "%" + frm.getAddress() + "%"));
            }
            if (frm.getEnabled() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("enabled"), frm.getEnabled()));
            }
            if (!StringUtils.isEmpty(frm.getUsername())) {
                predicateList.add(criteriaBuilder.like(root.get("username"), "%" + frm.getUsername() + "%"));
            }
            if (!StringUtils.isEmpty(frm.getPassword())) {
                predicateList.add(criteriaBuilder.like(root.get("password"), "%" + frm.getPassword() + "%"));
            }
            if (!StringUtils.isEmpty(frm.getAvatar())) {
                predicateList.add(criteriaBuilder.like(root.get("avatar"), "%" + frm.getAvatar() + "%"));
            }
            if (!StringUtils.isEmpty(frm.getRemark())) {
                predicateList.add(criteriaBuilder.like(root.get("remark"), "%" + frm.getRemark() + "%"));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeEnd()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeEnd()));
            }
            return criteriaBuilder.and(predicateList.toArray(new Predicate[0]));
        };
    }

    /**
     * Frm转Sort排序条件
     *
     * @param frm 排序参数对象
     * @return 返回JPA所需的排序对象
     */
    public static Sort getSort(SysUserFrm frm) {
        List<SortFieldFrm> sortFields = frm.getSortFields();
        List<Sort.Order> orderList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sortFields)) {
            for (SortFieldFrm sortFrm : sortFields) {
                if (StringUtils.isEmpty(sortFrm.getOrder())) {
                    continue;
                }
                orderList.add(new Sort.Order(Objects.equals(sortFrm.getOrder().toUpperCase(), "ASC") ? Sort.Direction.ASC : Sort.Direction.DESC, sortFrm.getPropName()));
            }
        }
        return Sort.by(orderList);
    }

}